<template>
  <div class="legal-page">
    <!-- 专利table -->
    <template v-if="type == 'patent'">
      <el-table
        :data="legalData"
        :empty-text="emptyText"
        :row-style="{ 'text-align': 'center' }"
        :header-row-style="{ 'text-align': 'center' }"
        class="legal-list"
      >
        <el-table-column prop="code" label="法律状态编码"> </el-table-column>
        <el-table-column prop="value" label="法律状态名称"> </el-table-column>
        <el-table-column prop="date" label="法律状态生效日期">
        </el-table-column>
        <el-table-column prop="title" label="说明"> </el-table-column>
      </el-table>
    </template>
    <template v-if="type == 'paper'">
      <el-table :data="legalData" :empty-text="emptyText" class="legal-list">
        <el-table-column prop="title_zh" label="标题"> </el-table-column>
        <el-table-column prop="venue_name" label="期刊"> </el-table-column>
        <el-table-column prop="authorStr" label="作者">
          <template #default="scope">
            <p v-html="scope.row.authorStr"></p>
          </template>
        </el-table-column>
        <el-table-column prop="organStr" label="机构"> </el-table-column>
      </el-table>
    </template>
  </div>
</template>

<script>
export default {
  props: {
    publicInfo: {
      type: Object,
    },
    type: {
      type: String,
    },
    id: {
      type: String,
    },
  },
  data() {
    return {
      legalData: [],
      emptyText: "数据加载中...",
      paramsType: "",
    };
  },
  methods: {
    // 不同业务调用不同的接口名称
    setLeftListName(type) {
      switch (type) {
        case "patent":
          this.paramsType = "patent";
          break;
        case "paper":
          this.paramsType = "paper";
          break;
        case "achieve":
          this.paramsType = "achieve";
          break;
        case "project":
          this.paramsType = "project";
          break;
        case "standard":
          this.paramsType = "standard";
          break;
        default:
          break;
      }
    },
    init() {
      let type = this.$route.query.type;
      this.setLeftListName(type);
      if (type == "patent") {
        this.legalData = this.publicInfo.leg_all;
      } else {
        this.$get(this.$Url.search.recommendById, {
          id: this.id,
        })
          .then((res) => {
            let list = res.data.result;
            list.map((li) => {
              const { authorStr, organStr } = this.getAuthors(li.authors);
              li.authorStr = authorStr;
              li.organStr = organStr;
            });
            this.legalData = list;
            this.emptyText = "";
          })
          .catch((err) => {});
      }
    },
    getAuthors(list) {
      let authorStr = "",
        organList = [],
        organStr = "";
      list.forEach((li, idx) => {
        if (li.org_zh || li.org) {
          if (organList.indexOf(li.org_zh || li.org) == -1) {
            organList.push(li.org_zh || li.org);
          }
          authorStr += `${idx > 0 ? "，" : ""}${li.name_zh || li.name}<sup>${
            organList.indexOf(li.org_zh || li.org) + 1
          }</sup>`;
        } else {
          authorStr += `${idx > 0 ? "，" : ""}${li.name_zh || li.name}`;
        }
      });
      organList.forEach((list, idx) => {
        organStr += `${idx == 0 ? "" : "，"}${idx + 1}.${list}`;
      });
      if (authorStr == "") {
        authorStr = "--";
      }
      if (organStr == "") {
        organStr = "--";
      }
      return { authorStr, organStr };
    },
  },
  mounted() {
    this.init();
  },
};
</script>

<style lang="scss" scoped>
.legal-page {
  display: flex;
  max-height: 100%;
  overflow-y: auto;
  .legal-list {
    flex: 1;
    .content {
      margin-top: 0;
      margin-bottom: 0;
    }
  }
}
</style>